package org.example.userdata;

import org.example.ConnectDatabase;

import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AddUser extends ConnectDatabase {

    int isOK = 1;
    public int insertUser(User user){
        new ConnectDatabase();
        try {
            String SQL ="insert into Users values(?,?,?,?)";
            PreparedStatement sta  = con.prepareStatement(SQL);
            //从左向右数第1个通配符?的值是user.getID():
            sta.setString(1, user.getID());
            sta.setString(2, user.getName());
            sta.setBoolean(3, user.getUser_Class() );
            //从左向右数第4个通配符?的值是user.getPassword():
            sta.setString(4, user.getPassword());
            isOK = sta.executeUpdate();
            con.close();
        }
        catch(SQLException e) {
            isOK = 0; //Users表中ID字段是主键，即不允许ID重复
        }
        return isOK;
    }
}
